<meta charset="utf-8"/>
<div class="row">
  <div class="col-xs-12">
    <div class="row">
      <div class="col-xs-12">
        <div style="height:36px;margin-top:2px">
          <table class="no-border" style="display:inline">
            <tr>
              <td permission='integral_btn_listData' style="padding-right:6px;display:none">
                申请人
              </td>
              <td permission='integral_btn_listData' style="display:none">
                <input type="text" id="key_real_name" class="form-control search-query" placeholder="输入申请人姓名" style="width:170px"/>
              </td>
              <td permission='integral_btn_listData' style="padding-right:6px;padding-left:6px;display:none">
                身份证号码
              </td>
              <td permission='integral_btn_listData' style="display:none">
                <input type="text" id="key_id_card" class="form-control search-query" placeholder="身份证号码" style="width:160px"/>
              </td>
              <td permission='integral_btn_listData' style="padding:0 6px;display:none" class="hidden-480">
                兑换状态
              </td>
              <td permission='integral_btn_listData' style="display:none" class="hidden-480">
                <span class="input-group-btn">
                  <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0px;">
                    <span id="labelSearch_state">全部状态</span>
                    <i class="ace-icon fa fa-angle-down"></i>
                  </button>
                  <ul class="dropdown-menu col-sm-12" style="width:99px;min-width:99px;">
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','','#labelSearch_state','全部状态');">全部状态</a>
                    </li>
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','1','#labelSearch_state','未兑换');">未兑换</a>
                    </li>
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','2','#labelSearch_state','已兑换');">已兑换</a>
                    </li>
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','3','#labelSearch_state','待兑换');">待兑换</a>
                    </li>
                  </ul>
                  <input id="key_state" type="hidden"/>
                </span>
              </td>
              <td permission='integral_btn_listData' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnSearch" title="搜索查询"><span class="ace-icon fa fa-search bigger-110"></span>搜索</button></span>
                </div>
              </td>
              <td permission='integral_br_exchange' style="display:none" class="hidden-480">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnExchange" title="兑换|批量兑换"><span class="ace-icon fa fa-money red2 bigger-110"></span>兑换</button></span>
                </div>
              </td>
            </tr>
          </table>
          <div class="hidden-480 pull-right tableTools-container" permission='integral_btn_listData' style="display:none;margin-bottom:2px"></div>
        </div>
        <table id="tableListUserIntegral" class="table table-striped table-bordered table-hover">
          <tbody>
          <tr>
            <td style="text-align:center;height:37px">正在加载……</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<div id="divKeyColumns" style="display:none;padding:1px 0px;"></div>
<div style="display:none;">
  <input type="hidden" id="integral_btn_listData"/>
  <input type="hidden" id="integral_row_queryById"/>
  <input type="hidden" id="integral_br_exchange"/>
</div>
<script type="text/javascript">
  ;(function($){
    var tableDom = '#tableListUserIntegral';
    var urlRoute = '/userIntegral/';/*请求controller层的url*/
    var getList = urlRoute + 'listData';/*获取数据列表*/
    var urlAdd = urlRoute + 'add';/*添加*/
    var urlEdit = urlRoute + 'edit';/*编辑*/
    var urlQueryById = urlRoute + 'queryById';/*根据id查询对应的数据*/
    var urlDelById = urlRoute + 'delById';/*根据id删除对应的数据*/
    var urlEditIntegralStatus = urlRoute + 'editIntegralStatus';/*兑换*/
    var stateOriginal = {};//仅供参考，更新优化-选项1,可参考项目law-frontend的页面serviceAd.html和后端law-backend的ServiceADService.java;记录编辑的值,仅在编辑时可用
    var thisTable = null;
    thisPage = {
      init : function(){
        this.initTable();
        this.createDropdown();
        this.initDom();
        this.addEvent();
      },
      initDom : function(){},
      createDropdown: function(){
      },
      addEvent: function(){
        $('#btnAdd').on('click', function(){
          thisPage.edit();
        });
        //搜索按钮
        $('#btnSearch').on('click', function(){
          thisPage.search();
        });
        //批量兑换
        $('#btnExchange').on('click', function(){
          thisPage.handlerExchange();
        });
        //重置按钮
        //按钮组-自定义显示列
        $('#iconColumn').parent().parent().on('click', function(){
          pageDataTable.columnCustom(thisTable, tableDom,'#divKeyColumns');
        });
        //按钮组-搜索
        $('#iconRefresh').parent().parent().on('click', function(){
          thisPage.search();
        });
      },
      /**获取已选中的主键集合,用法:var kids = thisPage.getKeyIds();if(kids == null){layerFn.alert('请选择要操作的数据', AppKey.code.code199);return;}*/
      getKeyIds : function(){
        var arrays = thisTable.rows({selected : true}).data();//获取已勾选的数据行
        if(arrays.length == 0){
          return null;
        }
        var kids = '';
        for(var i = 0; i < arrays.length; i++){
          var value = arrays[i].user_id;
          if(value != ''){
            if(kids.length > 0) kids += ",";
            kids += value;
          }
        }
        return kids;
      },
      /*批量兑换*/
      handlerExchange: function(){
        if(verifyFn.inputCheck('#integral_br_exchange','没有操作权限')) return;
        var kids = '';
        var index = 0;
        $(tableDom + ' tbody input:checked').each(function(){
          index++;
          var value = this.value;
          if(value != null && value != ''){
            if(kids.length > 0) kids += ",";
            kids += value;
          }
        });
        if(index == 0){
          layerFn.alert('请选择要兑换的数据!', AppKey.code.code199);
          return;
        }
        layerFn.confirm('将要为[' + index + ']条数据兑换积分,确定要继续吗?', function(){
          layerFn.submit(urlEditIntegralStatus,{ids:kids},function(data){
            thisPage.complete(data,null,true);
          });
        });
      },
      initTable : function(){
        var optsVisible = getOptsVisible('integral_row,integral_br');//确定是否要显示操作选项
        var optsBatchBtn = getOptsVisible('integral_br_exchange');//是否有批量操作的权限,若是多个按钮权限时以逗号,隔开
        var aoColumns = [];
        if(optsBatchBtn){
          aoColumns.push({
            sortable : false,
            data : "user_id",
            width : "38px",
            class : "center",
            visible : true,
            title : '<label title="全选|不选"><input type="checkbox" class="ace" /><span class="lbl"></span></label>'
          });
        }
        aoColumns.push({
          data : "real_name",
          sortable : false,
          title : "申请人"
        });
        aoColumns.push({
          data : "type",
          sortable : true,
          title : "身份类型",
          render : function(value,type,row,meta){
            if(value){
              switch (value){
                case 1:
                  return "省分公司";
                case 2:
                  return "市分公司";
                case 3:
                  return "区级代理";
                case 4:
                  return "直营门店";
                default:
                  return "";
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "user_name",
          sortable : true,
          width : "130px",
          title : "登录账号"
        });
        aoColumns.push({
          data : "id_card",
          sortable : false,
          width : "160px",
          title : "身份证号码"
        });
        aoColumns.push({
          data : "bank_code",
          sortable : false,
          width : "160px",
          title : "银行卡号"
        });
        aoColumns.push({
          data : "bank_name",
          sortable : false,
          title : "开户行"
        });
        aoColumns.push({
          data : "total_fee",
          sortable : false,
          width : "160px",
          title : "销售额",
          render : function(value,type,row,meta){
            switch (row.type){
              case 1:
                return "0.00";
              case 2:
                return "0.00";
              case 3:
              case 4:
                return value;
              default:
                return "0.00";
            }
          }
        });
        aoColumns.push({
          data : "score",
          sortable : false,
          width : "160px",
          title : "积分数"
        });
        aoColumns.push({
          data : "state",
          width : "86px",
          sortable : true,
          title : "兑换状态",
          className : 'hidden-480',
          render : function(value,type,row,meta){
            if(value){
              switch (value){
                case 1:
                  return "未兑换";
                case 2:
                  return "<span style='color:#1ec417'>已兑换</span>";
                case 3:
                  return "<span style='color:#f00'>待兑换</span>";
                default:
                  return "";
              }
            }else{
              return "";
            }
          }
        });
        if(optsVisible){
          aoColumns.push({
            data : "_user_id_",
            width : "75px",
            title : "<label style='color:#000;margin-left:6px;' title='单行操作'>操作选项</label>",/*此处的字‘操作选项’请勿修改,此处的文字还有其它用途*/
            sortable : false
          });
        }
        var columnDefs = [];
        if(optsBatchBtn){
          columnDefs.push({
            targets : 0,
            render : function(value,type,row,meta){
              return '<label title="选择|取消"><input type="checkbox" name="user_id" value="'+ value +'" style="cursor:pointer;text-decoration:none;outline:none;"/><span class="lbl"></span></label>';
            }
          });
        }
        if(optsVisible){//是否来显示某列
          columnDefs.push({
            targets : -1,
            render : function(value,type,row,meta){
              return "<a title='兑换' href='javascript:thisPage.rowEdit("+ meta.row +");' style='outline:none;text-decoration: none;color:#3b8cff;margin-left:4px;"+(row.state != 3 ? 'cursor:not-allowed;' : '')+"display:none' permission='integral_br_exchange'>兑换</a>";
            }
          });
        }
        columnDefs.push({
          targets : optsBatchBtn ? [1,2,3,4,5,6,7,8,9] : [0,1,2,3,4,5,6,7,8,9],
          render : function(value,type,row,meta){
            return pageDataTable.formatColumn(value);
          }
        });
        columnDefs.push({
          visible : false,//列的隐藏显示
          targets : []//指定列索引
        });
        thisTable = pageDataTable.initDataTable({
          tableDom : tableDom,
          sAjaxSource : getList,
          fnServerParams : function(params){
            params.push(
              {"name":"id_card","value":winFn.getDomValue('#key_id_card')},
              {"name":"real_name","value":winFn.getDomValue('#key_real_name')},
              {"name":"state","value":winFn.getDomValue('#key_state')}
            );
          },
          aoColumns : aoColumns,
          columnDefs : columnDefs,
          complete : function(settings,data){},
          selected : function(selected){}
        });
        pageDataTable.buttons(thisTable);
        pageDataTable.action(thisTable);
        pageDataTable.select(thisTable, tableDom);
        pageDataTable.tooltip();
        $('.dataTables_scroll .dataTables_scrollBody').removeAttr('style');//解决dataTable表头下拉框被tbody遮挡
      },
      search: function(){
        if(verifyFn.inputCheck('#integral_btn_listData','没有操作权限')) return;
        $(tableDom + '_wrapper table input[type=checkbox]').prop('checked',false);
        thisTable.draw();
      },
      resetForm: function(){
        winFn.clearFormData('#form_user_integral_edit',[{'dropdownLabel_state':'选择兑换状态'}]);
        /*
        $('#score').val('');
        winFn.addDropdown('#dropdownLabel_state','选择兑换状态');//新的置空方法已处理
        */
      },
      edit: function(user_id){
        var title = '添加';
        if(user_id != null && user_id.length > 0){
          if(verifyFn.inputCheck('#integral_row_queryById','没有操作权限')) return;
          title = '编辑';
          layerFn.queryByIdHint(urlQueryById, user_id, function(data){
            thisPage.openDialog(title, user_id, data);
            //thisPage.editJson(data.data,['ad_img']);//仅在赋值后再执行此代码,更新优化-选项2
          });
        }else{
          thisPage.openDialog(title);
        }
      },
      //移除不需要对比的key(此方法可有可无)
      editJson : function(data,keys){
        if(keys){
          for(var h = 0; h < keys.length; h++){
            delete data[keys[h]];//删除key元素
          }
        }
        stateOriginal = data;
      },
      //对比是否已修改了值,仅需判断必填的字段，文件上传的字段单独处理
      filterValue : function(params){
        if(stateOriginal != null){
          var ignoreKeys = '';
          var requiredKeys = '';
          for(var so in stateOriginal){
            if(params[so] == stateOriginal[so]){
              delete params[so];
              if(ignoreKeys != ''){
                ignoreKeys += ','+so;
              }else{
                ignoreKeys += so;
              }
            }else{
              if(requiredKeys != ''){
                requiredKeys += ','+so;
              }else{
                requiredKeys += so;
              }
            }
          }
          if(ignoreKeys != ''){
            params['ignoreKeys'] = ignoreKeys;//记录后端忽略验证的key以,隔开的字符串,注意数据库字段是否允许为空,可参考项目law-backend的joins.html;law-backend的JoinsService的方法edit();
          }
          if(requiredKeys != ''){
            params['requiredKeys'] = requiredKeys;//记录后端必填验证的key以,隔开的字符串
          }
        }
      },
      openDialog: function(title, user_id, map){
        var url = urlAdd;
        if(user_id != null && user_id.length > 0){
          url = urlEdit;
          if(verifyFn.inputNull('#integral_row_edit','没有编辑权限')) return;
        }else{
          if(verifyFn.inputNull('#integral_btn_add','没有添加权限')) return;
        }
        layerFn.addOrEdit(title,'#div_user_integral_edit', ['450px','166px'], function(layerIndex,layero){
          if(verifyFn.inputNull('#score','积分数不能为空')) return;
          if(verifyFn.inputNull('#state','请选择兑换状态')) return;
          var params = {
            score : winFn.getDomValue('#score',true),
            state : winFn.getDomValue('#state',true),
            user_id : user_id
          };
          /*thisPage.filterValue(params);//检查是否已修改了数据,更新优化-选项3,如果没有上传功能则可以验证未修改任何值时则不提交
          var validTotal = 0;
          for(var ck in params){
            if(params[ck] != undefined){
              validTotal++;
            }
          }*/
          /*if(validTotal == 0){//没有文件上传时使用此段代码,更新优化-选项4
            layerFn.alert('尚未修改任何数据', AppKey.code.code199);
            return;
          }*/
          layerFn.submit(url,params,function(data){
            thisPage.complete(data,layerIndex,true);
          });
        });
        thisPage.resetForm();
        if(map != null){
          $.each(map.data,function(k,v){
            try{
              if(k == 'state'){
                winFn.editDropdown('#dropdownLabel_state','state' + v);
                return true;
              }
              $('#' + k).val(v);//若是有问题则把这个放在最前面
            }catch(e){}
          });
        }
      },
      rowEdit : function(rowIndex){
        var row = thisTable.row(rowIndex).data();
        var _state = row.state;
        if(_state == 1){
          layerFn.alert('未申请兑换',AppKey.code.code199);
          return;
        }
        if(_state == 2){
          layerFn.alert('已兑换',AppKey.code.code199);
          return;
        }
        var _real_name = row.real_name ? row.real_name : '暂无';
        var _id_card = row.id_card ? row.id_card : '暂无';
        layerFn.confirm('您确定要为姓名<span style="color:#00B83F">'+_real_name+'</span>身份证<span style="color:#00B83F">'+_id_card+'</span>登录帐号<span style="color:#00B83F">'+row.user_name+'</span>兑换积分<span style="color:#00B83F">'+row.score+'</span>吗？',function(){
          layerFn.submit(urlEditIntegralStatus,{ids:row.user_id},function(data){
            thisPage.complete(data,null,true);
          });
        });
      },
      rowDel : function(rowIndex){
        if(verifyFn.inputCheck('#integral_row_delById','没有删除操作权限')) return;
        var row = thisTable.row(rowIndex).data();
        layerFn.confirm('删除后无法恢复,确定要删除吗?', function(){
          layerFn.delByIdHint(urlDelById, row.user_id,function(data){
            thisPage.complete(data,null,true);
          });
        });
      },
      //查看隐藏部分是全部内容,用法 render : function(value,type,row,meta){return thisPage.rowColumnView(value);}
      rowColumnView : function(value){
        if(value == null || value == undefined) return '';
        //value = typeof value === 'string' ? value.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;') : value;//好像有bug
        var len = value.length;
        if(len > 30){
          return "<a href='javascript:layerFn.viewValue(\""+value+"\",\"430px\");' title='点击查看详情' style='outline:none;text-decoration: none;'>"+value.substring(0,30)+"…</a>";
        }else{
          return value;
        }
      },
      complete: function(data,index,search){
        try{
          if(index){
            layerFn.closeIndex(index);
          }
          if(search){
            thisPage.search();
          }
          if(data){
            layerFn.handleResult(data.msg,data.code);
          }
        }catch(e){}
      }
    };
    thisPage.init();
    $('script').remove();
  })(jQuery);
</script>